/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.faytechcc.csc251.gui;

import edu.faytechcc.csc251.db.MTGDBQuery;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

/**
 *
 * @author woodsk9710
 */
class FilterPanel
        extends JPanel {

    private String[] filterArray = {
        "Artifact",
        "Creature",
        "Enchantment",
        "Instant",
        "Land",
        "Planeswalker",
        "Sorcery",
        "Tribal"
    };
    private JCheckBox[] filterBoxes = new JCheckBox[filterArray.length];
    private JButton filterButton;

    public FilterPanel() {
        this.setLayout(new GridLayout(3,
                0));
        this.setBorder(BorderFactory.createTitledBorder("Filter"));
        for (int i = 0;
                i < filterBoxes.length;
                i++) {
            filterBoxes[i] = new JCheckBox();
            filterBoxes[i].setText(filterArray[i]);
            this.add(filterBoxes[i]);
        }
        filterButton = new JButton("Filter");
        this.add(filterButton);
    }

    public JCheckBox[] getFilterBoxes() {
        return filterBoxes;
    }

    public void addActionListenerToFilterButton(FilterButtonListener listener) {
        filterButton.addActionListener(listener);
    }
}

class FilterButtonListener
        implements ActionListener {

    //private String[][] data;
    private ArrayList<String[]> dataList;
    private JCheckBox[] checkBox;
    private ResultsPanel rPanel;

    public FilterButtonListener(JCheckBox checkBox[], ResultsPanel rPanel) {
        this.rPanel = rPanel;
        this.checkBox = checkBox;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String query = "SELECT * FROM Card WHERE Type LIKE";
        for (JCheckBox checkBox1
                : checkBox) {
            if (checkBox1.isSelected()) {
                query += " '%" + checkBox1.getText() + "%' OR Type LIKE";
            }
        }
        query = query.substring(0,
                query.length() - 12);
        System.out.println(query);
        MTGDBQuery dbQuery = new MTGDBQuery(query);
        //data = dbQuery.getData();
        dataList = dbQuery.getList();
        //rPanel.setModelData(data);
        rPanel.setModelList(dataList);

    }

//    public ArrayList<String[]> returnData() {
//        return this.dataList;
//    }
}